---
{% if allorgs | length > 0 %}
Organizations:
{%   for org in allorgs %}
  - &{{ org }}
    Name: {{ org }}
    ID: {{ org }}
    MSPDir: keyfiles/{{ org }}/msp
{%     if org in peerorgs %}
{%       if project_version is version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
    Policies:
        Readers:
            Type: Signature
            Rule: "OR('{{ org }}.admin', '{{ org }}.peer')"
        Writers:
            Type: Signature
            Rule: "OR('{{ org }}.admin', '{{ org }}.client')"
        Admins:
            Type: Signature
            Rule: "OR('{{ org }}.admin')"
{%       endif %}

    AnchorPeers:
{%       for peer in allpeers %}
{%         if peer.org == org and fabric.k8s.exposeserviceport == true %}
      - Host: {{ k8shosts[peer.name] }}
        Port: {{ k8sports[peer.name+':7051'] }}
{%         elif peer.org == org %}
      - Host: {{ peer.name }}
        Port: 7051
{%         endif %}
{%       endfor %}
{%     elif org in ordererorgs %}
{%       if project_version is version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
    Policies:
        Readers:
            Type: Signature
            Rule: "OR('{{ org }}.member')"
        Writers:
            Type: Signature
            Rule: "OR('{{ org }}.member')"
        Admins:
            Type: Signature
            Rule: "OR('{{ org }}.admin')"
{%       endif %}
{%     endif %}
{%   endfor %}
{% endif %}

{% if project_version is version_compare('1.1.0','>=') or 'stable' in project_version or project_version == 'latest' %}
Capabilities:
  Global: &ChannelCapabilities
{%   if project_version is version_compare('1.4.3','>=') %}
    V1_4_3: true
{%   elif project_version is version_compare('1.4.2','>=') and project_version is version_compare('1.4.3','<') %}
    V1_4_2: true
{%   elif project_version is version_compare('1.3.0','>=') and project_version is version_compare('1.4.2','<') %}
    V1_3: true
{%   elif project_version is version_compare('1.1.0','>=') and project_version is version_compare('1.3.0','<') %}
    V1_1: true
{%   endif %}

  Orderer: &OrdererCapabilities
{%   if project_version is version_compare('1.4.2','>=') %}
    V1_4_2: true
{%   else %}
    V1_1: true
{%   endif %}

  Application: &ApplicationCapabilities
{%   if project_version is version_compare('1.4.2','>=') %}
    V1_4_2: true
{%   elif project_version is version_compare('1.3.0','>=') and project_version is version_compare('1.4.1','<=') %}
    V1_3: true
{%   elif project_version is version_compare('1.2.0','>=') and project_version is version_compare('1.3.0','<') %}
    V1_2: true
{%   elif project_version is version_compare('1.1.0','>=') and project_version is version_compare('1.2.0','<') %}
    V1_1: true
{%   endif %}
{% endif %}

{% if allorderers is defined and ( allorderers|length > 0) %}
Orderer: &OrdererDefaults
  OrdererType: {{ fabric.consensus_type | default('kafka') }}
  Addresses:
{%   if fabric.k8s.exposeserviceport == true %}
{%     for orderer in allorderers %}
    - {{ k8shosts[orderer.name] }}:{{ k8sports[orderer.name+':7050'] }}
{%     endfor %}
{%   else %}
{%     for orderer in allorderers %}
    - {{ orderer.name }}:7050
{%     endfor %}
{%   endif %}

{%   if fabric.orderersettings is defined and fabric.orderersettings.config is defined %}
  {{ fabric.orderersettings.config | to_nice_yaml | indent(2, False) -}}
{%   endif %}

{%   if kafkals | length > 0 %}
  Kafka:
    Brokers:
{%     for item in kafkals %}
      - {{ item }}:9092
{%     endfor %}
{%   endif %}

{%   if (project_version is version_compare('1.4.1','>=') or ('stable' in project_version or 'latest' in project_version)) and fabric.consensus_type is defined and fabric.consensus_type == 'etcdraft' %}
  EtcdRaft:
    Consenters:
{%     for orderer in allorderers %}
      - Host: {{ orderer.name }}
        Port: 7050
        ClientTLSCert: keyfiles/{{ orderer.org }}/orderers/{{ orderer.name }}.{{ orderer.org }}/tls/server.crt
        ServerTLSCert: keyfiles/{{ orderer.org }}/orderers/{{ orderer.name }}.{{ orderer.org }}/tls/server.crt
{%     endfor %}
{%     if fabric.raftsettings is defined and fabric.consensus_type == 'etcdraft' %}
    {{ fabric.raftsettings | to_nice_yaml | indent(2, False) -}}
{%     endif %}
{%   endif %}

  Organizations:
{%   for org in ordererorgs %}
    - *{{ org }}
{%   endfor %}

{%   if project_version is version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
  Policies:
      Readers:
          Type: ImplicitMeta
          Rule: "ANY Readers"
      Writers:
          Type: ImplicitMeta
          Rule: "ANY Writers"
      Admins:
          Type: ImplicitMeta
          Rule: "MAJORITY Admins"
      BlockValidation:
          Type: ImplicitMeta
          Rule: "ANY Writers"
{%   endif %}

{%   if project_version is version_compare('1.1.0','>=') or 'stable' in project_version or project_version == 'latest' %}
  Capabilities:
    <<: *OrdererCapabilities
{%   endif %}
{% endif %}

Application: &ApplicationDefaults
  Organizations:
{% for org in peerorgs %}
    - *{{ org }}
{% endfor %}
{% if project_version is version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
  Policies:
      Readers:
          Type: ImplicitMeta
          Rule: "ANY Readers"
      Writers:
          Type: ImplicitMeta
          Rule: "ANY Writers"
      Admins:
          Type: ImplicitMeta
          Rule: "MAJORITY Admins"
{% endif %}

{% if project_version is version_compare('1.1.0','>=') or 'stable' in project_version or project_version == 'latest' %}
  Capabilities:
    <<: *ApplicationCapabilities
{% endif %}

{% if project_version is version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
Channel: &ChannelDefaults
    Policies:
        Readers:
            Type: ImplicitMeta
            Rule: "ANY Readers"
        Writers:
            Type: ImplicitMeta
            Rule: "ANY Writers"
        Admins:
            Type: ImplicitMeta
            Rule: "MAJORITY Admins"
    Capabilities:
      <<: *ChannelCapabilities
{% endif %}

Profiles:
  OrgChannel:
    Consortium: FabricConsortium
    Application:
      <<: *ApplicationDefaults
{% if (project_version is version_compare('1.4.1','>=') or ('stable' in project_version or 'latest' in project_version)) and fabric.consensus_type is defined and fabric.consensus_type == 'etcdraft' %}
    <<: *ChannelDefaults
    Orderer:
      <<: *OrdererDefaults
{% endif %}
  OrdererGenesis:
{% if project_version is version_compare('1.2.0','>=') or 'stable' in project_version or project_version == 'latest' %}
    <<: *ChannelDefaults
{% endif %}
{% if project_version is version_compare('1.1.0','>=') or 'stable' in project_version or project_version == 'latest' %}
    Capabilities:
      <<: *ChannelCapabilities
{% endif %}
{% if allorderers is defined and ( allorderers|length > 0) %}
    Orderer:
      <<: *OrdererDefaults
{% endif %}
    Consortiums:
      FabricConsortium:
        Organizations:
{%   for org in peerorgs %}
          - *{{ org }}
{%   endfor %}
